package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.ProductCache;
import org.springframework.beans.factory.annotation.Autowired;

import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;

/**
 * 资费计划服务信息-区域编码属性
 */
public class FeePlanBaseIdAttrHandler extends AbstractAttrHandler<KiteProdInstFunc, AttrData> {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Override
    public void after(KiteAttr attr, KiteProdInstFunc inst, AttrData data) {
        KiteProdInst kiteProdInst = this.querySceneInstService.getProdInstByFuncProdInstId(data.getCustId(), inst.getProdInstId());
        List<KiteProdInstFunc> kiteProdInstFuncs = this.querySceneInstService.listProdInstFuncs(data.getCustId(), kiteProdInst.getProdInstId());
        KiteProdInstFunc kiteProdInstFunc = kiteProdInstFuncs.stream()
            .filter(prodInstFunc -> KiteStringUtils.equalsAny(ProductCache.get(prodInstFunc.getSpecId()).getProdNbr(), LocalKeyConsts.FUNC_PROD_ID_ZFFW, LocalKeyConsts.FUNC_PROD_ID_YF5GZF_BASE)).findFirst().orElse(null);
        if (Objects.isNull(kiteProdInstFunc)) {
            return;
        }
        KiteAttr kiteAttr = kiteProdInstFunc.getAttr("200004129");
        this.set(attr, inst, Optional.ofNullable(kiteAttr).map(KiteAttr::getValue).orElse(""), data);
    }

}
